package com.intelligentclass.controller;

import com.auth0.jwt.exceptions.TokenExpiredException;
import com.auth0.jwt.interfaces.DecodedJWT;
import com.intelligentclass.constant.UserType;
import com.intelligentclass.entity.Teaching;
import com.intelligentclass.exception.UserTypeException;
import com.intelligentclass.mydata.MyJson;
import com.intelligentclass.services.CourseService;
import com.intelligentclass.services.TeacherService;
import com.intelligentclass.utils.JWTUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin(origins = "*")
public class TeacherController {

    private final TeacherService teacherService;
    private final CourseService courseService;

    @Autowired
    public TeacherController(TeacherService teacherService,
                             CourseService courseService) {
        this.teacherService = teacherService;
        this.courseService = courseService;
    }

    @GetMapping("/api/teacher/getTeachingList")
    public ResponseEntity<?> getTeachingList(@RequestHeader Map<String, String> headers) {

        String username;
        MyJson json = new MyJson();
        DecodedJWT jwt;
        String token = headers.get("token");
        try{
            jwt = JWTUtil.verifyToken(token);
            username = jwt.getClaim("username").asString();
            int userType = jwt.getClaim("usertype").asInt();

            if(userType != UserType.TEACHER){
                throw new UserTypeException("Insufficient permission");
            }
        }
        catch(UserTypeException e){
            json.setMessage(e.getMessage());
            json.setStatus(400);
            json.addData("status", -1);
            return ResponseEntity.ok().body(json);
        }
        catch(TokenExpiredException e){
            json.setMessage("Token is expired");
            json.setStatus(400);
            json.addData("status", -1);
            return ResponseEntity.ok().body(json);
        }
        catch(Exception e){
            json.setMessage("Unexpected error");
            json.setStatus(400);
            json.addData("status", -1);
            return ResponseEntity.ok().body(json);
        }

        List<Teaching> list = teacherService.getTeachingList(username);
        List<TeachingItem> items = new ArrayList<>();
        for(Teaching teaching : list){
            items.add(new TeachingItem(teaching.getCid(), courseService.getCnameByCno(teaching.getCno()), teaching.getCno()));
        }
        json.setMessage("Success");
        json.setStatus(400);
        json.addData("TeachingList", items);
        return ResponseEntity.ok().body(json);
    }

    @GetMapping("/api/teacher/getIntroduce")
    public ResponseEntity<?> getIntroduce(@RequestHeader Map<String, String> headers){
        MyJson json = new MyJson();
        String token = headers.get("token");
        DecodedJWT jwt;
        String username;
        try{
            jwt = JWTUtil.verifyToken(token);
            username = jwt.getClaim("username").asString();
            int usertype = jwt.getClaim("usertype").asInt();
            if(usertype != UserType.TEACHER){
                json.setMessage("Insufficient permission");
                json.setStatus(400);
                json.addData("status", -1);
                return ResponseEntity.ok().body(json);
            }
        }
        catch(TokenExpiredException e){
            json.setMessage("Token is expired");
            json.setStatus(400);
            json.addData("status", -1);
            return ResponseEntity.ok().body(json);
        }
        catch(Exception e){
            json.setMessage("Unexpected error");
            json.setStatus(400);
            json.addData("status", -1);
            return ResponseEntity.ok().body(json);
        }

        String introduce = teacherService.getIntroduce(username);
        if(introduce == null){
            json.setMessage("Success");
            json.setStatus(200);
            json.addData("Introduce", "这个人很懒，什么都没留下...");
            return ResponseEntity.ok().body(json);
        }

        json.setMessage("Success");
        json.setStatus(400);
        json.addData("Introduce", teacherService.getIntroduce(username));
        return ResponseEntity.ok().body(json);
    }

    private static class TeachingItem{
        public int cid;
        public String name;
        public String cno;

        public TeachingItem(int cid, String name, String cno){
            this.cid = cid;
            this.name = name;
            this.cno = cno;
        }
    }
}
